** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D12.smcl", replace

// Baseline
use "$project_path/data/4_regdata/regdata", clear
rdrobust std_diff_m1_hdi runvar, all

// With region and decade fixed effects
use "$project_path/data/4_regdata/regdata", clear
gen decade = Year - mod(Year, 10)
tab decade, gen(d_decade_)
tab Region, gen(d_region_)
rdrobust std_diff_m1_hdi runvar, covs(d_decade_* d_region_*) all

// 3 post election years
use "$project_path/data/4_regdata/robustness/regdata_3_post_years", clear
rdrobust std_diff_m1_hdi runvar, all

// 5 post election years
use "$project_path/data/4_regdata/robustness/regdata_5_post_years", clear
rdrobust std_diff_m1_hdi runvar, all

// 7 post election years
use "$project_path/data/4_regdata/robustness/regdata_7_post_years", clear
rdrobust std_diff_m1_hdi runvar, all

// 10 post election years
use "$project_path/data/4_regdata/robustness/regdata_10_post_years", clear
rdrobust std_diff_m1_hdi runvar, all

// With year 0 in the post-election period
use "$project_path/data/4_regdata/robustness/regdata_with_year_0", clear
rdrobust std_diff_m1_hdi runvar, all

// Twice the MSE-optimal bandwidth
use "$project_path/data/4_regdata/regdata", clear
rdbwselect std_diff_m1_hdi runvar
local twice_bw = e(h_mserd)*2
rdrobust std_diff_m1_hdi runvar, h(`twice_bw') all

// Half the MSE-optimal bandwidth
use "$project_path/data/4_regdata/regdata", clear
rdbwselect std_diff_m1_hdi runvar
local half_bw = e(h_mserd)/2
rdrobust std_diff_m1_hdi runvar, h(`half_bw') all

// Second-order polynomial
use "$project_path/data/4_regdata/regdata", clear
rdrobust std_diff_m1_hdi runvar, p(2) all

// Uniform kernel
use "$project_path/data/4_regdata/regdata", clear
rdrobust std_diff_m1_hdi runvar, kernel(uni) all

// Epanechnikov kernel
use "$project_path/data/4_regdata/regdata", clear
rdrobust std_diff_m1_hdi runvar, kernel(epa) all

log close


** Building .tex table

//Prepare matrix of coefficients and other statistics
local nrows 12
local ncols 3
mat Coefs = J(`nrows', `ncols', .)

//Initialize row index
local i = 1
//Regressions
local outcome std_diff_m1_hdi

foreach Y in `outcome'{
	
	
	// Baseline
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, all
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// With region and decade fixed effects
	use "$project_path/data/4_regdata/regdata", clear
	gen decade = Year - mod(Year, 10)
	tab decade, gen(d_decade_)
	tab Region, gen(d_region_)
	rdrobust `Y' runvar, covs(d_decade_* d_region_*) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	//x post election years
	local post_election_year 3 5 7 10 
	foreach year in `post_election_year'{
		
		use "$project_path/data/4_regdata/robustness/regdata_`year'_post_years", clear
		rdrobust `Y' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''
		
		local ++i
	
	}
	
	
	// With year 0 in the post-election period
	use "$project_path/data/4_regdata/robustness/regdata_with_year_0", clear
	rdrobust `Y' runvar, all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Twice the MSE-optimal bandwidth
	use "$project_path/data/4_regdata/regdata", clear
	rdbwselect `Y' runvar
	local twice_bw = e(h_mserd)*2
	rdrobust `Y' runvar, h(`twice_bw') all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Half the MSE-optimal bandwidth
	use "$project_path/data/4_regdata/regdata", clear
	rdbwselect `Y' runvar
	local half_bw = e(h_mserd)/2
	rdrobust `Y' runvar, h(`half_bw') all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Second-order polynomial
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, p(2) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Uniform kernel
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, kernel(uni) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
	
	// Epanechnikov kernel
	use "$project_path/data/4_regdata/regdata", clear
	rdrobust `Y' runvar, kernel(epa) all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	
	//Fill the matrix
	mat Coefs[`i',1] = `b_`i''
	mat Coefs[`i',2] = `se_`i''
	mat Coefs[`i',3] = `p_`i''
	
	local ++i
}
********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 est
rename Coefs2 std_err
rename Coefs3 pval

gen outcome = ""

replace outcome = "Baseline" in 1
replace outcome = "With region and decade FE" in 2
replace outcome = "3 post-election years" in 3
replace outcome = "5 post-election years" in 4
replace outcome = "7 post-election years" in 5
replace outcome = "10 post-election years" in 6
replace outcome = "With year 0 in the post-election period" in 7
replace outcome = "Twice the MSE-optimal bandwidth" in 8
replace outcome = "Half the MSE-optimal bandwidth" in 9
replace outcome = "Second-order polynomial" in 10
replace outcome = "Uniform kernel" in 11
replace outcome = "Epanechnikov kernel" in 12

order outcome, first

tostring est, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace est = est + "***" if pval < `3_star'
replace est = est + "**" if  pval < `2_star' & pval > `3_star'
replace est = est + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}
replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"


label var outcome "Robustness check"
label var pval "p-val."
label var std_err "SE"
label var est "Est."

//Prepare texfile

local title "Robustness checks: Human Development Index"
local label_key "tab:robustness_checks_chix_social"
local output "$project_path/output/appendix_tables/Appendix_Table_D12.tex"
local footnote = "\emph{Notes}: This table reports results for the exercises of Table \ref{tab:robustness_checks_chix_econ} for HDI."

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{9.8cm} p{2cm} p{2cm} p{2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 


